package com.vogue.supplymall.common.domain.tool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.sms.client.SMS;
import com.sms.client.SMSServiceClient;
import org.apache.commons.lang.StringUtils;
/**
 * Created by 崔岳 on 2017-12-1.
 * 短信工具类
 */
@Component
public class SMSUtils {

	public static Logger logger = LoggerFactory.getLogger(SMSUtils.class);
	
//	private final static String SHOP_CODE = "5193dd24-6587-4e9a-9018-b8c390f7b56e";
	//正式
//	private final static Integer VCODE_JOIN_POINT_ID = 206;
	//测试
//	private final static Integer VCODE_JOIN_POINT_ID = 2001;

//	private final static String SMS_MD5_KEY = "szslsms";
	
	//吾享短信接口秘钥
	//正式
//	private final static String PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJ4Gs04zGYt0gkOeirpecGRmZgPgoAcXojR1RtGpCRs3QeKAjNmIIq0fgB+5twR36Uh2VjvZ3hm/lkjaSLVPcxrjXqu1FOtYXfpyh2UwKJacOIUvOUgitA/ReOGX1GYmEBSuHpXtfE4eA/Gw/tVns1zV8ThjhXWJhygAtlfIjtMfAgMBAAECgYAw+jKokf3g0wNsob6tdIexFmuX2A7BI/oJ+C8y3AkOflnVMbJ12VOmBJVZP94RbHjJFKQ1eJj1sgOKIN1YFH+/3abF5oRvx0QSx8bnOKoSB7WQ5KsbTkG0KcBcTYhVjQXpfBh0yVJ0rOJnibRlQgq535UjidnEOZ15YDvsUUefAQJBAP1MIDXegcCjLxqw5HQj+EmIFQUv59I5K6g/btgabvsr3ouuO5eqwTsEZJZcyryUaeqLXtNb3R4q0fE9D9oUNbECQQCftlgb+B9wKCp5sdB+f0qDZM9q6zqwTbCyFzEJRZJ5fjF4nyAitXHflOvUaGGzsP9+aoylEVH/24rSaBVAMznPAkAFidmWc/QQW0EXa1DDfVnbeb9D6sU94oQi3Kg2z3UnxtDJ6xhge5pYCQOOCh9gujoKp9t98Z9H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SAGmzzSeGa0Cw0v";
	//测试
//	private final static String PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKws4+G99KQCps03pjzgmD9Ma4gd/y+tpo2EAo4sPb8m4w9WHQD+tGI9yuX+ZmDs3tiSfFaV/5xwU0I3ZwcgWkn6B8sD+JVt8HhxlfP7n1RieY4Pqr58aixNeSbkF4IY2r8jhZr23Gnceb1pzbo4Lqotf4UcdJMP0oeP/lxgDy9DAgMBAAECgYBG4hBLbq3aG/FaCXAaHgjv/mFC376gfWhVWKTIrXQaT0MpnDVCpJnU3EW9HFg/zSUNmnkwGH7sg93zI+X5rBsfLLdVQ5bvl7McoeTdcTXPQg8csIZy+tXA+Jvr95x8KRL46/ETwVioPa9FcRZjrq3GPXhKDspQPFceLc8arXvPAQJBAPSy77AUdeZEm8NfpFWt1nxI9uhbHuOh/9fgGgNpjnzyoUBQID2rAASCnLls5rJZCGz25B9QpS+OCipC6Oq6AYECQQC0IILBqF/fZGTiCo7bjI1lA7a+OnXFMtKJYbW7vODuVqw9O4kkMlGZHtMvsSLfIfluCeP5dwRKVRH5IMoW0QrDAkBU/Nj2xQR155pZ7xv6jhgSrpzi8tL1qMxdQ/6E9OKCTxrmZW3tPTkSV/LrDAHMK9QbVFzC65WTwLeTGg4fiF8BAkEAo9yLdubYSJ3I1IUsTne/TSyiusztxDYmpwm3TJFXwhog2Bdn/AC2kGglUFaBVaFPipX+yTtHMOPQ1OTKUxVaBwJAL9u3DiKk8L6Ztoe09Q5L3+uLAAUIeZV/yRQ7th06d7d2zsCcSOs9djg2LWabIrHb2rerF34/TVNzryomHhARjA==";
	
	//发送短信接口
	//正式
//	private final static String SEND_URL = "http://sms.wuuxiang.com/rest/sms/send";
	//测试
//	private final static String SEND_URL = "http://192.168.12.202:8087/SMS_WebService/rest/sms/send";
	
	
	private static String shop_code;

	private static Integer vcode_join_point_id;
	
	private static String sms_md5_key;

	private static String private_key;

	private static String send_url;
	/**
	 * 发送短信至指定手机
	 * 
	 * @param tel	接收短信的手机号码
	 * @param msg	发送的短信内容
	 * @throws Exception 
	 */
	public static boolean sendMessage(String tel, String msg) throws Exception {
		logger.debug(
				String.format("向【%s】发送短信：%s", tel, msg));
		logger.info(
				String.format("短信参数[SHOP_CODE]:%s,"
						+ "[VCODE_JOIN_POINT_ID]:%s,"
						+ "[SMS_MD5_KEY]:%s,"
						+ "[PRIVATE_KEY]:%s,"
						+ "[SEND_URL]:%s", 
						shop_code, 
						vcode_join_point_id.toString(),
						sms_md5_key,
						private_key,
						send_url));
		SMS sms = new SMS(msg, tel, vcode_join_point_id, shop_code);
		SMSServiceClient client = new SMSServiceClient();
		client.setPrivate_key(private_key);
		client.setSend_url(send_url);
		client.setSms_md5_key(sms_md5_key);
		try {
			// 调用发送方法
			String result = client.asyncSend(sms);
			if (StringUtils.isNotEmpty(result)) {
				return true;
			}
		} catch (Exception e) {
			logger.error("发送短信验证码异常" + e.getMessage(), e);
			throw e;
		}
		return false;
//		return true;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			SMSUtils.sendMessage("18722239686", "恭喜食尚购已经大获成功~");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getShop_code() {
		return shop_code;
	}

	@Value("${smsutils.shop_code}") 
	public void setShop_code(String shop_code) {
		SMSUtils.shop_code = shop_code;
	}

	public Integer getVcode_join_point_id() {
		return vcode_join_point_id;
	}

	@Value("${smsutils.vcode_join_point_id}") 
	public void setVcode_join_point_id(Integer vcode_join_point_id) {
		SMSUtils.vcode_join_point_id = vcode_join_point_id;
	}

	public String getSms_md5_key() {
		return sms_md5_key;
	}

	@Value("${smsutils.sms_md5_key}") 
	public void setSms_md5_key(String sms_md5_key) {
		SMSUtils.sms_md5_key = sms_md5_key;
	}

	public String getPrivate_key() {
		return private_key;
	}

	@Value("${smsutils.private_key}") 
	public void setPrivate_key(String private_key) {
		SMSUtils.private_key = private_key;
	}

	public String getSend_url() {
		return send_url;
	}

	@Value("${smsutils.send_url}") 
	public void setSend_url(String send_url) {
		SMSUtils.send_url = send_url;
	}
	
}
